package com.marketingexpert.controller.streammgnt;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.User;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.WebUtils;



public class RepostWeiboController implements Controller {

	private SnsServiceFactory snsServiceFactory = null;
	
	private UserService userService;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/text;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter(); 	
		
		String sid =request.getParameter("sid");
		String status = request.getParameter("status");
		String userId = request.getParameter("userId");
		
		User u = userService.queryUserByUId(userId);
		
		int isComment = 0;
		String isCommentParam = request.getParameter("isComment");
		
		if(isCommentParam != null) {
			isComment = Integer.parseInt(isCommentParam);
		}
		
		SnsService snsService = null;
		
		snsService = WebUtils.getSnsService(u, getSnsServiceFactory());
		
		String result = snsService.repostWeibo(sid, status, isComment,u.getAccessToken(),u.getSecret());
		out.print(result);
		return null;
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	

}
